***Importing and Formatting Data
cd "C:\Users\gnv85086\Dropbox\Redistricting and the State Supremes\For Garrett\"

clear
collect clear
use "Final Replication Data.dta"

xtset st_code

sort state year docketno judge_id
order state year docketno judge_id

***Table 1 - Frequency Table for Party Dynamics in Observations
table (judge_party) (leg_party), missing name(descript) nototal

**Style Choices for Table 1
collect style header judge_party, title(hide)
collect style header leg_party, title(hide)
collect style cell border_block, border(right, pattern(nil))
collect style putdocx, title("Table 1: Number of votes cast in redistricting cases in state supreme courts, 1960-2022")
collect preview

collect export "Table 1.docx", replace

***Table 2 - Descriptive Stats for IV's
table (var) (result missingpartyvote[0]), stat(mean JudgePartyVoteGNV ffe_numeric extreme_pajid  i.select_numeric_OG i.judge_leg_factor part_gerr judge_party INIT_APP UP_ELECT)

**Style Choices for Table 2
collect style header result missingpartyvote, title(hide)
collect style cell border_block, border(right, pattern(nil))
collect style cell, nformat(%5.3f)
collect style putdocx, title("Table 2: Means of Model Variables")
collect preview

collect export "Table 2.docx", replace


sum extreme_pajid, d

***Table 3 - Logit Model and Table 4 - ADE of Ideology
collect create Table3

preserve

collect _r_b _r_se, name(Table3) tag(Model[2]): xtlogit JudgePartyVoteGNV i.ffe_numeric i.select_numeric_OG##c.extreme_pajid  b2.judge_leg_factor i.judge_party part_gerr i.cyclenumeric, re vce(cluster st_code) 

collect create Table4

collect, name(Table4) tag(Model[1]): mchange extreme_pajid, at(select_numeric_OG=1) amount(sd)
collect, name(Table4) tag(Model[2]): mchange extreme_pajid, at(select_numeric_OG=2) amount(sd)
collect, name(Table4) tag(Model[3]): mchange extreme_pajid, at(select_numeric_OG=3) amount(sd)
collect, name(Table4) tag(Model[4]): mchange extreme_pajid, at(select_numeric_OG=4) amount(sd)

collect set Table3

collect AIC=r(S)[1,"AIC"], name(Table3) tag(Model[2]): estat ic

predict prob_3_1, pr

*gen correct_pred_3_1 = .
*replace correct_pred_3_1 = 1 if prob_3_1>0.5 & JudgePartyVoteGNV==1 & prob_3_1!=.
*replace correct_pred_3_1 = 1 if prob_3_1<0.5 & JudgePartyVoteGNV==0 & prob_3_1!=.
*replace correct_pred_3_1 = 0 if prob_3_1<0.5 & JudgePartyVoteGNV==1 & prob_3_1!=.
*replace correct_pred_3_1 = 0 if prob_3_1>0.5 & JudgePartyVoteGNV==0 & prob_3_1!=.

*collect PCP=r(mean), name(Table3) tag(Model[2]): summarize correct_pred_3_1

gen prob_correct_pred_3_1  = binomialp(1, JudgePartyVoteGNV, prob_3_1)

collect ePCP=r(mean), name(Table3) tag(Model[2]): summarize prob_correct_pred_3_1

sum JudgePartyVoteGNV if prob_3_1!=.

scalar define eventprob = r(mean)

gen prob_correct_pred_mode = .
replace prob_correct_pred_mode = eventprob if JudgePartyVoteGNV==1 & prob_3_1!=.
replace prob_correct_pred_mode = 1 - eventprob if JudgePartyVoteGNV==0 & prob_3_1!=.

sum prob_correct_pred_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_3_1 - prob_correct_pred_mode)/(1 - denom)

collect ePRE=r(mean), name(Table3) tag(Model[2]): summarize RE

restore
***Table 3

**Style Choices for Table 3
*Remove base
collect style showbase off
collect style showempty off
*Represent interactions with an "*"
collect style row stack, delimiter(" * ")
*Remove vertical lines on the right stide of a cell
collect style cell border_block, border(right, pattern(nil))
*Limit significant digits
collect style cell, nformat(%5.2f)
*Parentheses around standard errors
collect style cell result[_r_se], sformat("(%s)")
*Hide labels for coefficient and standard error
collect style header result, level(hide)
collect style header program_class, level(hide)
*Limit significant digits
collect style cell result[AIC BIC], nformat(%8.0f)
collect style cell result[N], nformat(%8.0f)
*Make sure AIC and Number of Observations has a name
collect style header result[AIC], level(label)
collect style header result[N], level(label)
collect style header result[ePCP], level(label)
collect style header result[ePRE], level(label)
*Add stars to significant coefficients
collect stars _r_p 0.001 "***" 0.01 "**" 0.05 "*", attach(_r_b)
*Add a Title and Note to table
collect style putdocx, title("Table 3: Predicting a Party-Aligned Vote, 1961-2022") note("Note: * p<0.05, ** p<0.01, ***p<0.001") note("Omits random effects estimates by state")
*Label the different models in the table
collect label values Model 1 "Coef (SE)" 2 "Coef (SE)"

**Make and export Table 3
collect layout (colname#result program_class[eclass]#result[N] result [PCP] result[ePCP] result[ePRE]) (Model[2]), name(Table3)

collect export "Table 3.docx", replace

***Table 4
collect set Table4

**Style Choices for Table 4
*Remove vertical lines on the right stide of a cell
collect style cell border_block, border(right, pattern(nil))
*Limit significant digits
collect style cell, nformat(%5.2f)
*Add a Title and Note to table
collect style putdocx, title("Table 4: Average Discrete Effect of Ideological Extremity on the Probability of a Party-Aligned Vote by Selection System") note("Note: * p<0.05, ** p<0.01, ***p<0.001") note("Results are calculated using the estimates in Table 2 and observed data values")
*Label the different models in the table
collect label values Model 1 "Appointment" 2 "Merit Selection" 3 "Nonpartisan Election" 4 "Partisan Election"
collect label values colname Change "Average Direct Effect"

**Make and export Table 4
collect layout (colname[Change p-value]) (Model), name(Table4)

collect export "Table 4.docx", replace

***Figure 1
xtlogit JudgePartyVoteGNV i.cyclenumeric i.ffe_numeric i.select_numeric_OG##c.extreme_pajid  b2.judge_leg_factor i.judge_party part_gerr, re vce(cluster st_code)

mchange judge_leg_factor

mgen, at(extreme_pajid=(0(5)50) select_numeric_OG=1) stub(sel1) predlabel(Appointment)
mgen, at(extreme_pajid=(0(5)50) select_numeric_OG=2) stub(sel2) predlabel(Merit Selection)
mgen, at(extreme_pajid=(0(5)50) select_numeric_OG=3) stub(sel3) predlabel(Nonpartisan Election)
mgen, at(extreme_pajid=(0(5)50) select_numeric_OG=4) stub(sel4) predlabel(Partisan Election)

graph twoway (connected sel1pr sel2pr sel3pr sel4pr sel1extreme_pajid, lpat(solid dash dot longdash) msym(C S T D)), ytitle("Predicted Probability") ysc(r(0 1)) ylabel(0(0.2)1)

graph export "Figure 1.jpg", replace

graph twoway (connected sel1pr sel4pr sel1extreme_pajid, lpat(solid dash dot longdash) msym(C S T D)), ytitle("Predicted Probability") ysc(r(0 1)) ylabel(0(0.2)1) title("Figure 1: Probability of a Party-Aligned Vote," "by Ideology and Selection System")

graph export "Figure 1 Sig.jpg", replace


***Appendix Table 1: Extra Controls

collect create TableA1 

collect _r_b _r_se, name(TableA1) tag(Model[1]): xtlogit JudgePartyVoteGNV i.ffe_numeric i.select_numeric_OG##c.extreme_pajid  b2.judge_leg_factor i.judge_party part_gerr i.cyclenumeric, re vce(cluster st_code) 

collect AIC=r(S)[1,"AIC"], name(TableA1) tag(Model[1]): estat ic

preserve

predict prob_a1_1, pr

gen correct_pred_a1_1 = .
replace correct_pred_a1_1 = 1 if prob_a1_1>0.5 & JudgePartyVoteGNV==1 & prob_a1_1!=.
replace correct_pred_a1_1 = 1 if prob_a1_1<0.5 & JudgePartyVoteGNV==0 & prob_a1_1!=.
replace correct_pred_a1_1 = 0 if prob_a1_1<0.5 & JudgePartyVoteGNV==1 & prob_a1_1!=.
replace correct_pred_a1_1 = 0 if prob_a1_1>0.5 & JudgePartyVoteGNV==0 & prob_a1_1!=.

collect PCP=r(mean), name(TableA1) tag(Model[1]): summarize correct_pred_a1_1

gen prob_correct_pred_a1_1  = binomialp(1, JudgePartyVoteGNV, prob_a1_1)

collect ePCP=r(mean), name(TableA1) tag(Model[1]): summarize prob_correct_pred_a1_1

sum JudgePartyVoteGNV if prob_a1_1!=.

scalar define eventprob = r(mean)

gen prob_correct_pred_mode = .
replace prob_correct_pred_mode = eventprob if JudgePartyVoteGNV==1 & prob_a1_1!=.
replace prob_correct_pred_mode = 1 - eventprob if JudgePartyVoteGNV==0 & prob_a1_1!=.

sum prob_correct_pred_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_a1_1 - prob_correct_pred_mode)/(1 - denom)

collect ePRE=r(mean), name(TableA1) tag(Model[1]): summarize RE

restore

collect _r_b _r_se, name(TableA1) tag(Model[2]): xtlogit JudgePartyVoteGNV i.ffe_numeric i.select_numeric_OG##c.extreme_pajid b2.judge_leg_factor i.judge_party part_gerr i.INIT_APP i.UP_ELECT i.cyclenumeric, re vce(cluster st_code) 

collect AIC=r(S)[1,"AIC"], name(TableA1) tag(Model[2]): estat ic

preserve

predict prob_a1_2, pr

gen correct_pred_a1_2 = .
replace correct_pred_a1_2 = 1 if prob_a1_2>0.5 & JudgePartyVoteGNV==1 & prob_a1_2!=.
replace correct_pred_a1_2 = 1 if prob_a1_2<0.5 & JudgePartyVoteGNV==0 & prob_a1_2!=.
replace correct_pred_a1_2 = 0 if prob_a1_2<0.5 & JudgePartyVoteGNV==1 & prob_a1_2!=.
replace correct_pred_a1_2 = 0 if prob_a1_2>0.5 & JudgePartyVoteGNV==0 & prob_a1_2!=.

collect PCP=r(mean), name(TableA1) tag(Model[2]): summarize correct_pred_a1_2

gen prob_correct_pred_a1_2  = binomialp(1, JudgePartyLeadVoteGNV, prob_a1_2)

collect ePCP=r(mean), name(TableA1) tag(Model[2]): summarize prob_correct_pred_a1_2

sum JudgePartyLeadVoteGNV if prob_a1_2!=.

scalar define eventprob = r(mean)

gen prob_correct_pred_mode = .
replace prob_correct_pred_mode = eventprob if JudgePartyLeadVoteGNV==1 & prob_a1_2!=.
replace prob_correct_pred_mode = 1 - eventprob if JudgePartyLeadVoteGNV==0 & prob_a1_2!=.

sum prob_correct_pred_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_a1_2 - prob_correct_pred_mode)/(1 - denom)

collect ePRE=r(mean), name(TableA1) tag(Model[2]): summarize RE

restore

**Create some stylistic choices for tables
*Remove base
collect style showbase off
collect style showempty off
*Represent interactions with an "*"
collect style row stack, delimiter(" * ")
*Remove vertical lines on the right stide of a cell
collect style cell border_block, border(right, pattern(nil))
*Limit significant digits
collect style cell, nformat(%5.2f)
*Parentheses around standard errors
collect style cell result[_r_se], sformat("(%s)")
*Hide labels for coefficient and standard error
collect style header result, level(hide)
collect style header program_class, level(hide)
*Limit significant digits
collect style cell result[AIC BIC], nformat(%8.0f)
collect style cell result[N], nformat(%8.0f)
*Make sure AIC and Number of Observations has a name
collect style header result[AIC], level(label)
collect style header result[N], level(label)
collect style header result[PCP], level(label)
collect style header result[ePCP], level(label)
collect style header result[ePRE], level(label)
*Add stars to significant coefficients
collect stars _r_p 0.001 "***" 0.01 "**" 0.05 "*", attach(_r_b)
*Add a Title and Note to table
collect style putdocx, title("Table A1: Logit Model of a Judge's Vote being Partisan, 1961-2022") note("Note: * p<0.05, ** p<0.01, ***p<0.001") note("Table omits estimates of random effects parameters for states") layout(autofitcontents)
*Label the different models in the table
collect label values Model 1 "Original" 2 "Extra Controls"

**Make and export the table
collect layout (colname#result program_class[eclass]#result[N] result[ePCP] result[ePRE]) (Model[1] Model[2]), name(TableA1)

collect export "Appendix Table 1.docx", replace


***Appendix Table 2: Ideology Robustness
collect create TableA2

preserve

gen extreme_ideo = extreme_pajid

label variable extreme_ideo "Judge Extremism"

collect _r_b _r_se, name(TableA2) tag(Model[1]): xtlogit JudgePartyVoteGNV i.ffe_numeric i.select_numeric_OG##c.extreme_ideo  b2.judge_leg_factor i.judge_party part_gerr i.cyclenumeric, re vce(cluster st_code) 

collect AIC=r(S)[1,"AIC"], name(TableA2) tag(Model[1]): estat ic

predict prob_a2_1, pr

gen prob_correct_pred_a2_1  = binomialp(1, JudgePartyVoteGNV, prob_a2_1)

collect ePCP=r(mean), name(TableA2) tag(Model[1]): summarize prob_correct_pred_a2_1

sum JudgePartyVoteGNV if prob_a2_1!=.

scalar define eventprob = r(mean)

gen prob_correct_pred_mode = .
replace prob_correct_pred_mode = eventprob if JudgePartyVoteGNV==1 & prob_a2_1!=.
replace prob_correct_pred_mode = 1 - eventprob if JudgePartyVoteGNV==0 & prob_a2_1!=.

sum prob_correct_pred_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_a2_1 - prob_correct_pred_mode)/(1 - denom)

collect ePRE=r(mean), name(TableA2) tag(Model[1]): summarize RE

restore

preserve

gen extreme_ideo = extreme_cf

label variable extreme_ideo "Judge Extremism"

collect _r_b _r_se, name(TableA2) tag(Model[2]): xtlogit JudgePartyVoteGNV i.ffe_numeric i.select_numeric_OG##c.extreme_ideo  b2.judge_leg_factor i.judge_party part_gerr i.cyclenumeric, re vce(cluster st_code) 

collect AIC=r(S)[1,"AIC"], name(TableA2) tag(Model[2]): estat ic

predict prob_a2_2, pr

gen correct_pred_a2_2 = .
replace correct_pred_a2_2 = 1 if prob_a2_2>0.5 & JudgePartyVoteGNV==1 & prob_a2_2!=.
replace correct_pred_a2_2 = 1 if prob_a2_2<0.5 & JudgePartyVoteGNV==0 & prob_a2_2!=.
replace correct_pred_a2_2 = 0 if prob_a2_2<0.5 & JudgePartyVoteGNV==1 & prob_a2_2!=.
replace correct_pred_a2_2 = 0 if prob_a2_2>0.5 & JudgePartyVoteGNV==0 & prob_a2_2!=.

collect PCP=r(mean), name(TableA2) tag(Model[2]): summarize correct_pred_a2_2

gen prob_correct_pred_a2_2  = binomialp(1, JudgePartyVoteGNV, prob_a2_2)

collect ePCP=r(mean), name(TableA2) tag(Model[2]): summarize prob_correct_pred_a2_2

sum JudgePartyVoteGNV if prob_a2_2!=.

scalar define eventprob = r(mean)

gen prob_correct_pred_mode = .
replace prob_correct_pred_mode = eventprob if JudgePartyVoteGNV==1 & prob_a2_2!=.
replace prob_correct_pred_mode = 1 - eventprob if JudgePartyVoteGNV==0 & prob_a2_2!=.

sum prob_correct_pred_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_a2_2 - prob_correct_pred_mode)/(1 - denom)

collect ePRE=r(mean), name(TableA2) tag(Model[2]): summarize RE

restore

preserve

gen extreme_ideo = extreme_whh

label variable extreme_ideo "Judge Extremism"

collect _r_b _r_se, name(TableA2) tag(Model[3]): xtlogit JudgePartyVoteGNV i.ffe_numeric i.select_numeric_OG##c.extreme_ideo  b2.judge_leg_factor i.judge_party part_gerr i.cyclenumeric, re vce(cluster st_code)

collect AIC=r(S)[1,"AIC"], name(TableA2) tag(Model[3]): estat ic

predict prob_a2_3, pr

gen correct_pred_a2_3 = .
replace correct_pred_a2_3 = 1 if prob_a2_3>0.5 & JudgePartyVoteGNV==1 & prob_a2_3!=.
replace correct_pred_a2_3 = 1 if prob_a2_3<0.5 & JudgePartyVoteGNV==0 & prob_a2_3!=.
replace correct_pred_a2_3 = 0 if prob_a2_3<0.5 & JudgePartyVoteGNV==1 & prob_a2_3!=.
replace correct_pred_a2_3 = 0 if prob_a2_3>0.5 & JudgePartyVoteGNV==0 & prob_a2_3!=.

collect PCP=r(mean), name(TableA2) tag(Model[3]): summarize correct_pred_a2_3

gen prob_correct_pred_a2_3  = binomialp(1, JudgePartyVoteGNV, prob_a2_3)

collect ePCP=r(mean), name(TableA2) tag(Model[3]): summarize prob_correct_pred_a2_3

sum JudgePartyVoteGNV if prob_a2_3!=.

scalar define eventprob = r(mean)

gen prob_correct_pred_mode = .
replace prob_correct_pred_mode = eventprob if JudgePartyVoteGNV==1 & prob_a2_3!=.
replace prob_correct_pred_mode = 1 - eventprob if JudgePartyVoteGNV==0 & prob_a2_3!=.

sum prob_correct_pred_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_a2_3 - prob_correct_pred_mode)/(1 - denom)

collect ePRE=r(mean), name(TableA2) tag(Model[3]): summarize RE

restore

preserve

gen extreme_ideo = extreme_pajid

label variable extreme_ideo "Judge Extremism"

collect _r_b _r_se, name(TableA2) tag(Model[4]): xtlogit JudgePartyVoteGNV i.ffe_numeric i.select_numeric_OG##c.extreme_ideo  b2.judge_leg_factor i.judge_party part_gerr i.cyclenumeric if whh!=., re vce(cluster st_code) 

collect AIC=r(S)[1,"AIC"], name(TableA2) tag(Model[4]): estat ic

predict prob_a2_1, pr

gen prob_correct_pred_a2_1  = binomialp(1, JudgePartyVoteGNV, prob_a2_1)

collect ePCP=r(mean), name(TableA2) tag(Model[4]): summarize prob_correct_pred_a2_1

sum JudgePartyVoteGNV if prob_a2_1!=.

scalar define eventprob = r(mean)

gen prob_correct_pred_mode = .
replace prob_correct_pred_mode = eventprob if JudgePartyVoteGNV==1 & prob_a2_1!=.
replace prob_correct_pred_mode = 1 - eventprob if JudgePartyVoteGNV==0 & prob_a2_1!=.

sum prob_correct_pred_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_a2_1 - prob_correct_pred_mode)/(1 - denom)

collect ePRE=r(mean), name(TableA2) tag(Model[4]): summarize RE

restore

preserve

gen extreme_ideo = extreme_cf

label variable extreme_ideo "Judge Extremism"

collect _r_b _r_se, name(TableA2) tag(Model[5]): xtlogit JudgePartyVoteGNV i.ffe_numeric i.select_numeric_OG##c.extreme_ideo  b2.judge_leg_factor i.judge_party part_gerr i.cyclenumeric if whh!=., re vce(cluster st_code) 

collect AIC=r(S)[1,"AIC"], name(TableA2) tag(Model[5]): estat ic

predict prob_a2_2, pr

gen correct_pred_a2_2 = .
replace correct_pred_a2_2 = 1 if prob_a2_2>0.5 & JudgePartyVoteGNV==1 & prob_a2_2!=.
replace correct_pred_a2_2 = 1 if prob_a2_2<0.5 & JudgePartyVoteGNV==0 & prob_a2_2!=.
replace correct_pred_a2_2 = 0 if prob_a2_2<0.5 & JudgePartyVoteGNV==1 & prob_a2_2!=.
replace correct_pred_a2_2 = 0 if prob_a2_2>0.5 & JudgePartyVoteGNV==0 & prob_a2_2!=.

collect PCP=r(mean), name(TableA2) tag(Model[5]): summarize correct_pred_a2_2

gen prob_correct_pred_a2_2  = binomialp(1, JudgePartyVoteGNV, prob_a2_2)

collect ePCP=r(mean), name(TableA2) tag(Model[5]): summarize prob_correct_pred_a2_2

sum JudgePartyVoteGNV if prob_a2_2!=.

scalar define eventprob = r(mean)

gen prob_correct_pred_mode = .
replace prob_correct_pred_mode = eventprob if JudgePartyVoteGNV==1 & prob_a2_2!=.
replace prob_correct_pred_mode = 1 - eventprob if JudgePartyVoteGNV==0 & prob_a2_2!=.

sum prob_correct_pred_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_a2_2 - prob_correct_pred_mode)/(1 - denom)

collect ePRE=r(mean), name(TableA2) tag(Model[5]): summarize RE

restore


**Create some stylistic choices for tables
*Remove base
collect style showbase off
collect style showempty off
*Represent interactions with an "*"
collect style row stack, delimiter(" * ")
*Remove vertical lines on the right stide of a cell
collect style cell border_block, border(right, pattern(nil))
*Limit significant digits
collect style cell, nformat(%5.2f)
*Parentheses around standard errors
collect style cell result[_r_se], sformat("(%s)")
*Hide labels for coefficient and standard error
collect style header result, level(hide)
collect style header program_class, level(hide)
*Limit significant digits
collect style cell result[AIC BIC], nformat(%8.0f)
collect style cell result[N], nformat(%8.0f)
*Make sure AIC and Number of Observations has a name
collect style header result[AIC], level(label)
collect style header result[N], level(label)
collect style header result[PCP], level(label)
collect style header result[ePCP], level(label)
collect style header result[ePRE], level(label)
*Add stars to significant coefficients
collect stars _r_p 0.001 "***" 0.01 "**" 0.05 "*", attach(_r_b)
*Add a Title and Note to table
collect style putdocx, title("Table A2: Logit Models of a Judge's Vote being Partisan, by Measure of Judicial Ideology") note("Note: * p<0.05, ** p<0.01, ***p<0.001") note("Table omits estimates of random effects parameters for states") layout(autofitcontents)
*Label the different models in the table
collect label values Model 1 "PAJID" 2 "CF" 3 "WHH" 4 "PAJID, WHH Sample" 5 "CF, WHH Sample"

**Make and export the table
collect layout (colname#result program_class[eclass]#result[N] result[ePCP] result[ePRE]) (Model[1] Model[2] Model[3] Model[4] Model[5]), name(TableA2)

collect export "Appendix Table 2.docx", replace name(TableA2)


***Appendix Table 3: Party Leadership BDV
label define judge_leg_label_rep 1 "Majority Judge" 2 "Opposition Judge" 3 "Divided Government"

collect create TableA3 

preserve

gen judge_leg_factor_custom = judge_leg_factor

label values judge_leg_factor_custom judge_leg_label_rep

collect _r_b _r_se, name(TableA3) tag(Model[1]): xtlogit JudgePartyVoteGNV i.ffe_numeric i.select_numeric_OG##c.extreme_pajid  b2.judge_leg_factor_custom i.judge_party part_gerr i.cyclenumeric, re vce(cluster st_code) 

collect AIC=r(S)[1,"AIC"], name(TableA3) tag(Model[1]): estat ic

predict prob_a3_1, pr

gen correct_pred_a3_1 = .
replace correct_pred_a3_1 = 1 if prob_a3_1>0.5 & JudgePartyVoteGNV==1 & prob_a3_1!=.
replace correct_pred_a3_1 = 1 if prob_a3_1<0.5 & JudgePartyVoteGNV==0 & prob_a3_1!=.
replace correct_pred_a3_1 = 0 if prob_a3_1<0.5 & JudgePartyVoteGNV==1 & prob_a3_1!=.
replace correct_pred_a3_1 = 0 if prob_a3_1>0.5 & JudgePartyVoteGNV==0 & prob_a3_1!=.

collect PCP=r(mean), name(TableA3) tag(Model[1]): summarize correct_pred_a3_1

gen prob_correct_pred_a3_1  = binomialp(1, JudgePartyVoteGNV, prob_a3_1)

collect ePCP=r(mean), name(TableA3) tag(Model[1]): summarize prob_correct_pred_a3_1

sum JudgePartyVoteGNV if prob_a3_1!=.

scalar define eventprob = r(mean)

gen prob_correct_pred_mode = .
replace prob_correct_pred_mode = eventprob if JudgePartyVoteGNV==1 & prob_a3_1!=.
replace prob_correct_pred_mode = 1 - eventprob if JudgePartyVoteGNV==0 & prob_a3_1!=.

sum prob_correct_pred_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_a3_1 - prob_correct_pred_mode)/(1 - denom)

collect ePRE=r(mean), name(TableA3) tag(Model[1]): summarize RE

restore

preserve

gen judge_leg_factor_custom = judge_leg_factor_inclusive

label values judge_leg_factor_custom judge_leg_label_rep

collect _r_b _r_se, name(TableA3) tag(Model[2]): xtlogit JudgePartyLeadVoteGNV i.ffe_numeric i.select_numeric_OG##c.extreme_pajid b2.judge_leg_factor_custom i.judge_party part_gerr i.cyclenumeric, re vce(cluster st_code) 

collect AIC=r(S)[1,"AIC"], name(TableA3) tag(Model[2]): estat ic

predict prob_a3_2, pr

gen correct_pred_a3_2 = .
replace correct_pred_a3_2 = 1 if prob_a3_2>0.5 & JudgePartyVoteGNV==1 & prob_a3_2!=.
replace correct_pred_a3_2 = 1 if prob_a3_2<0.5 & JudgePartyVoteGNV==0 & prob_a3_2!=.
replace correct_pred_a3_2 = 0 if prob_a3_2<0.5 & JudgePartyVoteGNV==1 & prob_a3_2!=.
replace correct_pred_a3_2 = 0 if prob_a3_2>0.5 & JudgePartyVoteGNV==0 & prob_a3_2!=.

collect PCP=r(mean), name(TableA3) tag(Model[2]): summarize correct_pred_a3_2

gen prob_correct_pred_a3_2  = binomialp(1, JudgePartyLeadVoteGNV, prob_a3_2)

collect ePCP=r(mean), name(TableA3) tag(Model[2]): summarize prob_correct_pred_a3_2

sum JudgePartyLeadVoteGNV if prob_a3_2!=.

scalar define eventprob = r(mean)

gen prob_correct_pred_mode = .
replace prob_correct_pred_mode = eventprob if JudgePartyLeadVoteGNV==1 & prob_a3_2!=.
replace prob_correct_pred_mode = 1 - eventprob if JudgePartyLeadVoteGNV==0 & prob_a3_2!=.

sum prob_correct_pred_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_a3_2 - prob_correct_pred_mode)/(1 - denom)

collect ePRE=r(mean), name(TableA3) tag(Model[2]): summarize RE

restore

preserve

gen judge_leg_factor_custom = judge_leg_factor

label values judge_leg_factor_custom judge_leg_label_rep

collect _r_b _r_se, name(TableA3) tag(Model[3]): xtologit JudgePartyVoteOrdinalGNV i.ffe_numeric i.select_numeric_OG##c.extreme_pajid  b2.judge_leg_factor_custom i.judge_party part_gerr i.cyclenumeric, vce(cluster st_code) 

collect AIC=r(S)[1,"AIC"], name(TableA3) tag(Model[3]): estat ic


predict pr1 pr2 pr3, pr

gen prob_correct_pred_a3_3 = .
replace prob_correct_pred_a3_3 = pr1 if JudgePartyVoteOrdinalGNV == 1 & pr1!=.
replace prob_correct_pred_a3_3 = pr2 if JudgePartyVoteOrdinalGNV == 2 & pr2!=.
replace prob_correct_pred_a3_3 = pr3 if JudgePartyVoteOrdinalGNV == 3 & pr3!=.

collect ePCP=r(mean), name(TableA3) tag(Model[3]): summarize prob_correct_pred_a3_3 

gen JudgePartyVoteOrdinalGNV1 = .
replace JudgePartyVoteOrdinalGNV1=1 if JudgePartyVoteOrdinalGNV==1
replace JudgePartyVoteOrdinalGNV1=0 if JudgePartyVoteOrdinalGNV!=1

sum JudgePartyVoteOrdinalGNV1 if prob_correct_pred_a3_3!=.

scalar define eventprob1 = r(mean)

gen JudgePartyVoteOrdinalGNV2 = .
replace JudgePartyVoteOrdinalGNV2=1 if JudgePartyVoteOrdinalGNV==2
replace JudgePartyVoteOrdinalGNV2=0 if JudgePartyVoteOrdinalGNV!=2

sum JudgePartyVoteOrdinalGNV2 if prob_correct_pred_a3_3!=.

scalar define eventprob2 = r(mean)

gen JudgePartyVoteOrdinalGNV3 = .
replace JudgePartyVoteOrdinalGNV3=1 if JudgePartyVoteOrdinalGNV==3
replace JudgePartyVoteOrdinalGNV3=0 if JudgePartyVoteOrdinalGNV!=3

sum JudgePartyVoteOrdinalGNV3 if prob_correct_pred_a3_3!=.

scalar define eventprob3 = r(mean)

gen prob_correct_mode = .
replace prob_correct_mode=eventprob1 if JudgePartyVoteOrdinalGNV==1
replace prob_correct_mode=eventprob2 if JudgePartyVoteOrdinalGNV==2
replace prob_correct_mode=eventprob3 if JudgePartyVoteOrdinalGNV==3

sum prob_correct_mode

scalar define denom = r(mean)

gen RE = (prob_correct_pred_a3_3 - prob_correct_mode)/(1 - denom)

collect ePRE=r(mean), name(TableA3) tag(Model[3]): summarize RE

restore

**Create some stylistic choices for tables
*Remove base
collect style showbase off
collect style showempty off
*Represent interactions with an "*"
collect style row stack, delimiter(" * ")
*Remove vertical lines on the right stide of a cell
collect style cell border_block, border(right, pattern(nil))
*Limit significant digits
collect style cell, nformat(%5.2f)
*Parentheses around standard errors
collect style cell result[_r_se], sformat("(%s)")
*Hide labels for coefficient and standard error
collect style header result, level(hide)
collect style header program_class, level(hide)
*Limit significant digits
collect style cell result[AIC BIC], nformat(%8.0f)
collect style cell result[N], nformat(%8.0f)
*Make sure AIC and Number of Observations has a name
collect style header result[AIC], level(label)
collect style header result[N], level(label)
collect style header result[PCP], level(label)
collect style header result[ePCP], level(label)
collect style header result[ePRE], level(label)
*Add stars to significant coefficients
collect stars _r_p 0.001 "***" 0.01 "**" 0.05 "*", attach(_r_b)
*Add a Title and Note to table
collect style putdocx, title("Table A3: Logit Model of a Judge's Vote being Partisan, by alternative Measures of Party Voting, 1961-2022") note("Note: * p<0.05, ** p<0.01, ***p<0.001") note("Table omits estimates of random effects parameters for states") layout(autofitcontents)
*Label the different models in the table
collect label values Model 1 "Original" 2 "Binary with Split" 3 "Ordinal with Split"

**Make and export the table
collect layout (colname#result program_class[eclass]#result[N] result[ePCP] result[ePRE]) (Model[1] Model[2] Model[3]), name(TableA3)

collect export "Appendix Table 3.docx", replace
